# coding=utf-8

"""
    Created by Kalyter on 2017-12-15.
"""

import xlrd

from learning.scientifc.readers.reader import Reader


class XLSReader(Reader):
    """Excel文件读取器"""

    def __init__(self, *args, **kwargs):
        super(XLSReader, self).__init__(*args, **kwargs)
        self.xls_file = None
        self.table = None

    @property
    def nrows(self):
        return self.table.nrows

    @property
    def rows(self):
        if not self._rows:
            for _, item in self.range():
                self._rows.append(item)
        return self._rows

    def row_values(self, row):
        return self.table.row_values(row)

    def remove(self, index):
        self._rows.remove(self._rows[index])

    def range(self, start=0, end=None, step=1):
        if end is None:
            end = self.nrows
        for i in range(start, end, step):
            yield i, self.table.row_values(i)

    def load(self, **kwargs):
        self.xls_file = xlrd.open_workbook(self._fullname)
        self.table = self.xls_file.sheet_by_index(0)
